<template>
  <div class="select-tactic">
    <div class="search">
      <div class="icon">
        <img src="../../../../assets/ss.svg" alt="" />
      </div>
      <input type="text" v-model="search" placeholder="请输入武将名称" />
      <div class="clear" v-if="search" @click="search = ''">
        <img src="../../../../assets/clear.svg" alt="" />
      </div>
    </div>
    <collapse-transition>
      <div class="type" :class="{ pb: !filter.show }" v-show="!search.trim()">
        <div
          class="item"
          @click="selectType(1)"
          :class="{ active: filter.type.indexOf(1) !== -1 }"
        >
          指挥
        </div>
        <div
          class="item"
          @click="selectType(2)"
          :class="{ active: filter.type.indexOf(2) !== -1 }"
        >
          主动
        </div>
        <div
          class="item"
          @click="selectType(3)"
          :class="{ active: filter.type.indexOf(3) !== -1 }"
        >
          突击
        </div>
        <div
          class="item"
          @click="selectType(4)"
          :class="{ active: filter.type.indexOf(4) !== -1 }"
        >
          被动
        </div>
        <div
          class="item"
          @click="selectType(5)"
          :class="{ active: filter.type.indexOf(5) !== -1 }"
        >
          兵种
        </div>
        <div
          class="item"
          @click="selectType(6)"
          :class="{ active: filter.type.indexOf(6) !== -1 }"
        >
          阵法
        </div>
        <div
          class="filter"
          @click="filter.show = !filter.show"
          :class="{ active: filter.show }"
        >
          筛选<img
            src="../../../../assets/down.svg"
            onload="SVGInject(this)"
            class="down-icon"
            :class="{ active: filter.show }"
          />
        </div>
      </div>
    </collapse-transition>
    <collapse-transition>
      <div class="quality" v-show="filter.show && !search.trim()">
        <div class="tit">·武将品质</div>
        <div class="list">
          <div
            class="item"
            @click="selectQuality(4)"
            :class="{ active: filter.quality.indexOf(4) !== -1 }"
          >
            S
          </div>
          <div
            class="item"
            @click="selectQuality(3)"
            :class="{ active: filter.quality.indexOf(3) !== -1 }"
          >
            A
          </div>
          <div
            class="item"
            @click="selectQuality(2)"
            :class="{ active: filter.quality.indexOf(2) !== -1 }"
          >
            B
          </div>
          <div
            class="item"
            @click="selectQuality(1)"
            :class="{ active: filter.quality.indexOf(1) !== -1 }"
          >
            C
          </div>
        </div>
      </div>
    </collapse-transition>
    <div class="tactics-list">
      <vue-scroll :ops="ops">
        <div class="list">
          <div
            class="tactic"
            :class="{
              active: tacticIsSelect(tactic.id) === 'active',
              dis: tacticIsSelect(tactic.id) === 'disable',
            }"
            @click="clickTactic(tactic)"
            v-for="(tactic, i) in filterList"
            :key="i"
            v-html="tactic.name"
          ></div>
        </div>
      </vue-scroll>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import Tactic from "@/object/Tactic";
export default {
  name: "SelectTactic",
  props: {
    value: Object,
    fill: String,
  },
  model: { prop: "value", event: "change" },
  computed: {
    ...mapState("data", ["heroesList", "tacticsList"]),
    filterList() {
      if (this.search.trim()) {
        let list = this.tacticsList
          .map((item) => {
            return new Tactic(JSON.parse(JSON.stringify(item)));
          })
          .filter((item) => {
            let isBorn = false;
            if (
              item.born_tactic_heroes.length &&
              !item.from_tactic_events.length &&
              !item.inherit_tactic_heroes.length
            )
              isBorn = true;
            if (item.name.indexOf(this.search.trim()) !== -1 && !isBorn) {
              var reg = new RegExp(this.search.trim(), "g");
              item.name = item.name.replace(
                reg,
                "<span>" + this.search.trim() + "</span>"
              );
              return true;
            } else return false;
          });
        return list;
      } else {
        let list = this.tacticsList.map((item) => {
          return new Tactic(JSON.parse(JSON.stringify(item)));
        });

        // 战法类型
        if (this.filter.type.length) {
          list = list.filter((item) => {
            return this.filter.type.indexOf(item.type) !== -1;
          });
        }

        // 战法等级
        if (this.filter.quality.length) {
          list = list.filter((item) => {
            return this.filter.quality.indexOf(item.quality) !== -1;
          });
        }

        list = list.filter((item) => {
          let isBorn = false;
          if (
            item.born_tactic_heroes.length &&
            !item.from_tactic_events.length &&
            !item.inherit_tactic_heroes.length
          )
            isBorn = true;
          return !isBorn;
        });

        return list;
      }
    },
  },
  data() {
    return {
      search: "",
      ops: {
        scrollPanel: {
          scrollingX: false,
          scrollingY: true,
        },
        bar: {
          showDelay: 500,
          onlyShowBarOnScroll: true,
          keepShow: false,
          background: "#c1c1c1",
          opacity: 1,
          hoverStyle: false,
          specifyBorderRadius: false,
          minSize: false,
          size: "5px",
          disable: false,
        },
      },
      filter: {
        show: false,
        quality: [],
        type: [],
      },
    };
  },
  methods: {
    selectType(type) {
      if (this.filter.type.indexOf(type) === -1) {
        this.filter.type.push(type);
      } else {
        this.filter.type.splice(this.filter.type.indexOf(type), 1);
      }
    },
    selectQuality(quality) {
      if (this.filter.quality.indexOf(quality) === -1) {
        this.filter.quality.push(quality);
      } else {
        this.filter.quality.splice(this.filter.quality.indexOf(quality), 1);
      }
    },
    herosById(id) {
      let data;
      this.heroesList.some((item) => {
        if (item.id === id) data = item;
        return !!data;
      });
      return data || { born_tactic_ids: [null] };
    },
    tacticById(id) {
      let tactic;
      this.tacticsList.some((item) => {
        if (item.id === id) tactic = item;
        return !!tactic;
      });
      return tactic;
    },
    // 战法是否被选中
    tacticIsSelect(id) {
      // 获取战法详细数据
      let tactic = this.tacticById(id);
      let use_num = 0;

      // 获取3个武将自带战法
      let main_tactic = [
        this.herosById(this.value.chef_hero).born_tactic_ids[0],
        this.herosById(this.value.sub_hero_one).born_tactic_ids[0],
        this.herosById(this.value.sub_hero_two).born_tactic_ids[0],
      ];

      let heros_arr = [
        ["chef_tactic_one", "chef_tactic_two"],
        ["sub_one_tactic_one", "sub_one_tactic_two"],
        ["sub_two_tactic_one", "sub_two_tactic_two"],
      ];

      let select = "";
      heros_arr.forEach((item, index) => {
        if (item.indexOf(this.fill) !== -1) {
          // 如果 战法被当前武将的自带战法占位 在当前武将下无法选择该战法
          if (id === main_tactic[index]) select = "disable";
          else {
            // 判断战法是否被另外两个选中
            if (this.value[item[0]] === id) select = item[0];
            else if (this.value[item[1]] === id) select = item[1];

            // 判断战法是都是当前战法
            if (select) {
              // console.log(select, this.fill);
              select = select === this.fill ? "active" : "disable";
              use_num += 1;
            }
          }
        } else {
          if (this.value[item[0]] === id || this.value[item[1]] === id) {
            use_num += 1;
          }
        }
      });

      // select 为空 并且在其他武将中出现次数等于或大于了配置数 就返回禁用
      if (select !== "active" && use_num >= tactic.canbe_config) {
        return "disable";
      } else return select;
    },
    // 点击武将
    clickTactic(tactic) {
      // 判断武将是否被其他位置选中
      let select = this.tacticIsSelect(tactic.id);
      if (select && select === "disable") return;
      let value = JSON.parse(JSON.stringify(this.value));
      value[this.fill] = tactic.id;
      this.$emit("change", value);
      this.$emit("next");
    },
  },
};
</script>

<style lang="scss" scoped>
.select-tactic {
  flex: 1;
  height: 0;
  margin-top: 0.1rem;
  border-radius: 4px;
  border: 1px solid #ebebeb;
  padding: 0.1rem 0.1rem 0.01rem;
  display: flex;
  flex-direction: column;
  background: #fff;
  .search {
    height: 0.34rem;
    border-radius: 4px;
    border: 1px solid #ebebeb;
    display: flex;
    align-items: center;
    input {
      flex: 1;
      width: 0;
      border: none;
      font-size: 0.14rem;
      color: #233d4d;
      &::placeholder {
        font-size: 0.12rem;
        color: #cccccc;
      }
    }
    .icon {
      width: 0.34rem;
      height: 0.34rem;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: 0.24rem;
      }
    }
    .clear {
      width: 0.3rem;
      height: 0.34rem;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      img {
        width: 0.22rem;
      }
    }
  }
  .type {
    display: flex;
    align-items: center;
    margin-top: 0.07rem;
    position: relative;
    justify-content: space-between;
    padding-bottom: 0rem;
    &.pb {
      padding-bottom: 0.28rem;
    }
    .item {
      width: 0.45rem;
      height: 0.28rem;
      background: #f2f2f2;
      border-radius: 0.05rem;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 0.12rem;
      &.active {
        background: #fff;
        border: 1px solid #ff8a69;
        background-image: url(../../../../assets/right_close.svg);
        background-repeat: no-repeat;
        background-position: right top;
      }
    }
    .flex {
      flex: 1;
    }
    .filter {
      position: absolute;
      width: 0.56rem;
      height: 0.28rem;
      top: 0.34rem;
      right: 0;
      border-radius: 0.05rem;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid #ebebeb;
      font-size: 0.12rem;
      color: #233d4d;
      svg {
        margin-left: 0.06rem;
        width: 0.06rem;
        height: 0.04rem;
        transform: rotate(180deg);
        transition: all 0.3s;
      }
      &.active {
        background: #fff;
        color: #ff8a69;
        border: 1px solid #ff8a69;
        svg {
          transform: rotate(0deg);
        }
      }
    }
  }
  .quality {
    margin-top: 0.15rem;
    .tit {
      font-size: 0.12rem;
      color: #233d4d;
      line-height: 0.17rem;
    }
    .list {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 0.05rem;
      .item {
        width: 0.72rem;
        height: 0.28rem;
        background: #f2f2f2;
        border-radius: 0.05rem;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 0.12rem;
        &.active {
          background: #fff;
          border: 1px solid #ff8a69;
          background-image: url(../../../../assets/right_close.svg);
          background-repeat: no-repeat;
          background-position: right top;
        }
      }
    }
  }
  .tactics-list {
    margin-top: 0.07rem;
    height: 0;
    flex: 1;
    .tactic {
      width: 0.72rem;
      height: 0.28rem;
      background: #ffffff;
      border-radius: 4px;
      border: 1px solid #ebebeb;
      box-sizing: border-box;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-size: 0.12rem;
      margin: 0.08rem 0 0;
      margin-left: 0.06rem;
      color: #233d4d;
      &.tactic:nth-child(4n + 1) {
        margin-left: 0;
      }
      &.active {
        color: #fff !important;
        background: #ff8a69;
        border: 1px solid #ff8a69;
        ::v-deep {
          span {
            color: #fff !important;
          }
        }
      }
      &.dis {
        color: #fff;
        background: #cccccc;
        border: 1px solid #cccccc;
        ::v-deep {
          span {
            color: #fff !important;
          }
        }
      }
      ::v-deep {
        span {
          color: #ff8a69;
        }
      }
    }
  }
}
</style>
